package com.work.mdass.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.work.mdass.domain.OrderApply;
import com.work.mdass.properties.OrderApplyInterface;

@Service
public class OrderApplyService {
	
	@Autowired
	private OrderApplyInterface orderApplyInterface;
	
	/*
	 * 增加一条征订申请
	 */
	public OrderApply addOrderApply(OrderApply orderApply) {
		
		if(!isExit(orderApply.gettSerial())) {
			
			
			orderApply.setStatus("未审核");
			
			return orderApplyInterface.save(orderApply);
			
		}else
			return null;
	}
	
	/*
	 * 通过教师编号查找申请
	 */
	public Map<Object,Object> getOrderApplyByTS(Integer tSerial){
		
		List<OrderApply> applies =  orderApplyInterface.findByTSerial(tSerial);
		
		Map<Object,Object> result = new HashMap<>();
		
		if(applies.size()!=0) {
			result.put("code", 0);
			result.put("msg", "success");
			result.put("data", applies);
			result.put("count", applies.size());
		}else{
			result.put("code", 1);
			result.put("msg", "暂时没有符合条件的数据");
			result.put("data", applies);
			result.put("count", applies.size());
		}
		return result;
	}
	
	/*
	 * 通过关键字查找申请
	 */
	public Map<Object,Object> getOrderApplyByKW(Integer tSerial){
		
		List<OrderApply> applies =  orderApplyInterface.findByTSerial(tSerial);
		
		Map<Object,Object> result = new HashMap<>();
		
		if(applies.size()!=0) {
			result.put("code", 0);
			result.put("msg", "成功");
			result.put("data", applies);
			result.put("count", applies.size());
		}else{
			result.put("code", 1);
			result.put("msg", "没有符合条件的数据");
			result.put("data", applies);
			result.put("count", applies.size());
		}
		return result;
	}
	/*
	 * 通过申请编号获得
	 */
	public OrderApply getByApplySerial(Integer applySerial) {
		
		return orderApplyInterface.findByApplySerial(applySerial);
	}
	/*
	 * 获得所有的申请记录
	 */
	public Map<Object,Object> getAll(){
		
		List<OrderApply> orderApplies = new ArrayList<>();
		
		orderApplies = orderApplyInterface.findAll();
		
		Map<Object,Object> result = new HashMap<>();
		
		if(orderApplies.size()!=0) {
			result.put("code", 0);
			result.put("msg", "成功");
			result.put("data", orderApplies);
			result.put("count", orderApplies.size());
		}else {
			result.put("code", 1);
			result.put("msg", "没有符合条件的数据");
			result.put("data", orderApplies);
			result.put("count", orderApplies.size());
		}
		return result;
				
	}
	/*
	 * 审核
	 */
	public Map<Object,Object> updata(OrderApply orderApply) {
		
		OrderApply orderApplies =  orderApplyInterface.save(orderApply);
		
		List<OrderApply> order  = new ArrayList<>();
		
		order.add(orderApplies);
		
		Map<Object,Object> result = new HashMap<>();
		
		result.put("code", 1);
		result.put("msg", "success");
		result.put("data", order);
		result.put("count",1);
		return result;
		
	}
	/*
	 * 删除一条记录
	 */
	public OrderApply deleteByid(Integer id) {
		orderApplyInterface.delete(id);
		return null;
	}
	/*
	 * 检测是否已提交过该申请
	 */
	public boolean isExit(Integer applySerial){
		
		if(orderApplyInterface.findByApplySerial(applySerial)!= null) return true;
		else return false;
	}
}
